#include "zuoye.h"
//链表节点函数
int creat_node (node_t **p,int num){
    if(NULL == p){
        return -1;
    }
     *p = (node_t *) malloc(sizeof(node_t));
     if(NULL == *p){
        printf("内存分配失败\n");
        return -1;
     }
     (*p)->data = num;
     (*p)->next = NULL;
     return 0;
}
//头插函数
int insert_list_by_head (node_t *phead,int num){
    if(NULL == phead){
        return -1;
    }
//创建新的节点
    node_t *pnew = NULL;
    creat_node(&pnew,num);
//执行
    pnew->next = phead->next;
    phead->next = pnew;
    return 0;
}
//打印表中元素函数
    int print_list(node_t *phead){
        if(NULL == phead){
            return -1;
        }
        node_t *ptemp = phead->next;
        while(NULL != ptemp){
            printf("%d ",ptemp->data);
            ptemp = ptemp->next;
        }
        printf("\n");
        return 0;
    }
//尾插函数
    int insert_list_by_tail(node_t *phead,int num){
        if(NULL == phead){
            return -1;
        }
        node_t *ptemp = phead;
        while (NULL != ptemp->next)
        {
            ptemp = ptemp->next;
        }
        node_t *pnew = NULL;
        creat_node(&pnew,num);
        ptemp->next = pnew;
        return 0;
    }
//任意位置插入元素函数
    int insert_list_by_pos(node_t *phead,int pos,int num){
        if(NULL == phead){
            return -1;
        }
        node_t *ptemp = phead;
        if(pos<0){
            printf("位置不合理\n");
            return -1;
        }
        for(int i =0;i<pos;i++){
            if(NULL == ptemp->next){
                printf("位置错误\n");
                return -1;
            }
            ptemp = ptemp->next;
        }
        node_t *pnew = NULL;
        creat_node(&pnew,num);
        pnew->next = ptemp->next;
        ptemp->next = pnew;
        return 0;
    }
//头删函数
    int delete_list_by_head(node_t *phead){
        if(NULL == phead){
            return -1;
        }
        if(NULL == phead->next){
            printf("表空，无法删除\n");
            return -1;
        }
        node_t *pdel = phead->next;
        phead->next = phead->next->next;
        free(pdel);
        pdel = NULL;
        return 0;
    }
//尾删函数
    int delete_list_by_tail(node_t *phead){
        if(NULL == phead){
            return -1;
        }
        if(NULL == phead->next){
            printf("表空，无法删除\n");
            return -1;
        }
        node_t *ptemp = phead;
        while (NULL != ptemp->next->next)
        {
            ptemp = ptemp->next;
        }
        free(ptemp->next);
        ptemp->next = NULL;
        return 0; 
    }
//任意位置删函数
    int delete_list_by_pos(node_t *phead,int pos){
        if(NULL == phead){
            return -1;
        }
        if(NULL == phead->next){
            printf("表空，无法删除\n");
            return -1;
        }
        if(pos < 0){
            printf("位置不合理\n");
            return -1;
        }
        node_t *ptemp = phead;
        for(int i =0;i<pos;i++){
            while (NULL == ptemp->next->next)
            {
                printf("位置不合理\n");
                return -1;
            }
            ptemp = ptemp->next;
        }
        node_t *pdel = ptemp->next;
            ptemp->next = pdel->next;
            free(pdel);
            pdel = NULL; 
        
        return 0;
    }